package com.chenxin.wms.controller;

import com.chenxin.common.annotation.Log;
import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.core.page.TableDataInfo;
import com.chenxin.common.enums.BusinessType;
import com.chenxin.common.utils.poi.ExcelUtil;
import com.chenxin.wms.domain.StockEntity;
import com.chenxin.wms.domain.dto.BatchForbidRequestDTO;
import com.chenxin.wms.domain.vo.StockVO;
import com.chenxin.wms.process.stock.IStockProcess;
import com.chenxin.wms.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存查询Controller
 *
 * @author gry
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/wms/stock")
public class StockController extends BaseController {
    @Autowired
    private IStockService stockService;
    @Autowired
    private IStockProcess stockProcess;

    /**
     * 查询库存查询列表
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockVO stockVo) {
        startPage();
        List<StockVO> list = stockService.selectStockList(stockVo);
        return getDataTable(list);
    }

    /**
     * 超期库存管理
     */
    @GetMapping("/overdueList")
    public TableDataInfo overdueList(StockVO stockVo) {
        startPage();
        stockVo.setLockStatus(2); //超期状态为 2
        List<StockVO> list = stockService.selectStockList(stockVo);
        return getDataTable(list);
    }

    /**
     * 盘点库存查询列表
     */
    @GetMapping("/checkStockList")
    public TableDataInfo checkStockList(StockVO stockVo) {
        startPage();
        List<StockVO> list = stockService.checkStockList(stockVo);
        return getDataTable(list);
    }

    /**
     * 导出库存查询列表
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:export')")
    @Log(title = "库存查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockVO stockVo) {
        List<StockVO> list = stockService.selectStockList(stockVo);
        ExcelUtil<StockVO> util = new ExcelUtil<>(StockVO.class);
        util.exportExcel(response, list, "库存查询数据");
    }

    /**
     * 获取库存查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:stock:query')")
    @GetMapping(value = {"/", "/{stockId}"})
    public AjaxResult getInfo(@PathVariable(value = "stockId", required = false) Long stockId) {
        return success(stockService.selectStockByStockId(stockId));
    }

    /**
     * 获取可用库存信息
     */
    @GetMapping(value = {"/usableList"})
    public AjaxResult getUsable(@RequestParam("spuNo") String spuNo) {
        return success(stockService.selectStockBySpuNo(spuNo));
    }

    @PostMapping("/disables")
    @Log(title = "一键禁用(解禁)")
    public AjaxResult disable(@RequestBody BatchForbidRequestDTO request) {
        // 根据作业类型（冻结&解禁）处理相关库存
        try {
            request.setUserId(getUserId());
            // 1. 执行业务逻辑（禁用/解禁）
            stockProcess.stockDisables(request);
            // 2. 返回成功响应
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("msg", "操作成功");
            return AjaxResult.success(response);

        } catch (Exception e) {
            // 3. 返回失败响应
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("msg", e.getMessage());
            return AjaxResult.error("操作失败：" + e.getMessage(), errorResponse);
        }
    }

    @PostMapping("/directOut")
    @Log(title = "原材直发出库")
    public AjaxResult directOut(@RequestBody BatchForbidRequestDTO request) {
        try {
            // 1. 执行业务逻辑
            stockProcess.directOut(request);
            // 2. 返回成功响应
            Map<String, Object> response = new HashMap<>();
            response.put("code", 0);
            response.put("msg", "操作成功");
            return AjaxResult.success(response);
        } catch (Exception e) {
            // 3. 返回失败响应
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("msg", e.getMessage());
            return AjaxResult.error("操作失败：" + e.getMessage(), errorResponse);
        }
    }

}
